package asia.lxq.cic.service;

import asia.lxq.cic.config.GlobalConfig;
import asia.lxq.cic.dao.ArticleMapper;
import asia.lxq.cic.dao.CommentMapper;
import asia.lxq.cic.dao.UserMapper;
import asia.lxq.cic.entity.Article;
import asia.lxq.cic.entity.Result;
import asia.lxq.cic.entity.StatusCode;
import asia.lxq.cic.entity.User;
import asia.lxq.cic.util.RedisUtil;
import asia.lxq.cic.util.UserDirUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArticleService {

    @Autowired
    RedisUtil redisUtil;
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    CommentMapper commentMapper;

    public Result<List<Article>> getUserArticleList(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user= (User) redisUtil.get(GlobalConfig.PRE_USERINFO_REDIS+authentication.getPrincipal());
        Map<String,Object> selectMap=new HashMap<>();
        selectMap.put("article_author_id",user.getUserId());
        List<Article> articleList = articleMapper.selectByMap(selectMap);
        articleList.forEach(article -> article.query(userMapper,commentMapper,true));
        return Result.buildOrigin(StatusCode.OK,articleList,"成功",true);
    }

    public Result<Article> getArticleDetail(int articleId) {
        Article article = articleMapper.selectById(articleId);
        article.setArticleEye(article.getArticleEye()+1);
        article.query(userMapper,commentMapper,false);
        articleMapper.updateById(article);
        return Result.buildOrigin(StatusCode.OK,article,"成功",true);
    }

    public Result<Object> thumbUp(int articleId){
        Article article = articleMapper.selectById(articleId);
        article.setArticleThumbUp(article.getArticleThumbUp()+1);
        articleMapper.updateById(article);
        return Result.success("点赞成功");
    }

    //todo 需要判断用户的资源文件夹是否存在
    public Result<Object> updateArticle(Article article,MultipartFile resourceFile) {
        Article queriedArticle=articleMapper.selectById(article.getArticleId());
        if (resourceFile!=null){
            String resName="article_cover_"+queriedArticle.getArticleResourceId()+".png";
            try {
                resourceFile.transferTo(new File(UserDirUtil.getUserResDir(queriedArticle.getArticleAuthorId(),resName)));
            } catch (IOException e) {
                return Result.buildOrigin(StatusCode.OTHER_ERR,null,"保存文件时发生错误："+e.getMessage(),false);
            }
        }
        queriedArticle.setArticleTitle(article.getArticleTitle());
        queriedArticle.setArticleDesc(article.getArticleDesc());
        queriedArticle.setArticleContent(article.getArticleContent());
        queriedArticle.setArticleExtent(article.getArticleExtent());
        if (queriedArticle.getArticleExtent().equals("全国")){
            queriedArticle.setArticleCollegeId(0);
        }
        if (queriedArticle.getArticleExtent().equals("本校")){
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            User user= (User) redisUtil.get(GlobalConfig.PRE_USERINFO_REDIS+authentication.getPrincipal());
            queriedArticle.setArticleCollegeId(user.getCollegeId());
        }
        articleMapper.updateById(queriedArticle);
        return Result.buildOrigin(StatusCode.OK,article.getArticleId(),"修改成功",true);
    }
    public Result<Map<String, Object>> getArticleListOfArea(int collegeId, int page, int pageSize){
        QueryWrapper<Article> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("article_college_id",collegeId);
        if(collegeId==0){
            queryWrapper.or().eq("article_Extent","同时").or().eq("article_Extent","全国");
        }else{
            queryWrapper.or().eq("article_Extent","同时");
        }
        queryWrapper.eq("article_Status","已发布");
        queryWrapper.orderByDesc("article_date");
        Page<Article> pageInfo = articleMapper.selectPage(new Page<>(page, pageSize), queryWrapper);
        List<Article> articleList=pageInfo.getRecords();
        articleList.forEach(article -> article.query(userMapper,commentMapper,true));
        Map<String,Object> resultData=new HashMap<>();
        resultData.put("data",articleList);
        resultData.put("total",pageInfo.getTotal());
        resultData.put("current",pageInfo.getCurrent());
        resultData.put("pages",pageInfo.getPages());
        return Result.buildOrigin(StatusCode.OK, resultData, "成功", true);
    }
}
